//
//  PhotoVaultCell.m
//  ChatForHot
//
//  Created by FLY on 2018/10/15.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "PhotoVaultCell.h"
#import "PhotoVideoCell.h"
#import "XMPlayerView.h"

@interface PhotoVaultCell()<UICollectionViewDataSource,UICollectionViewDelegate>

@end
@implementation PhotoVaultCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        [self addSubview:self.HOTCHATimgView];
        [self addSubview:self.HOTCHATtitleLab];
        [self addSubview:self.HOTCHATlineView];
        [self addSubview:self.HOTCHATcollectionView];

        [self.HOTCHATimgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(15);
            make.left.equalTo(self).offset(15);
            make.width.height.mas_equalTo(16);
        }];
        [self.HOTCHATtitleLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self.HOTCHATimgView);
            make.left.equalTo(self.HOTCHATimgView.mas_right).offset(15);
        }];
        [self.HOTCHATlineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self);
            make.height.mas_equalTo(0.5);
        }];
        [self.HOTCHATcollectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.height.mas_equalTo(75);
            make.top.equalTo(self.HOTCHATimgView.mas_bottom).offset(15);
        }];

    }
    return self;
}
//lazzy
- (UIImageView *)HOTCHATimgView{
    if (_HOTCHATimgView == nil) {
        _HOTCHATimgView = [[UIImageView alloc] init];
        _HOTCHATimgView.contentMode = UIViewContentModeCenter;
        _HOTCHATimgView.image = [UIImage imageNamed:@"photovault"];
    }
    return _HOTCHATimgView;
}

- (UILabel *)HOTCHATtitleLab{
    if (_HOTCHATtitleLab == nil) {
        _HOTCHATtitleLab = [[UILabel alloc] init];
        _HOTCHATtitleLab.font = [UIFont systemFontOfSize:14];
        _HOTCHATtitleLab.textColor = HOTCHATHexColor(0x444444);
        _HOTCHATtitleLab.text = NSLocalizedString(@"StrN042", nil);
    }
    return _HOTCHATtitleLab;
}
- (UIView *)HOTCHATlineView{
    
    if (_HOTCHATlineView == nil) {
        
        _HOTCHATlineView = [[UIView alloc]init];
        _HOTCHATlineView.backgroundColor = HOTCHATHexColor(0xF0F0F0);
    }
    return _HOTCHATlineView;
}


- (UICollectionView *)HOTCHATcollectionView{
    if (_HOTCHATcollectionView == nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        
        _HOTCHATcollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _HOTCHATcollectionView.dataSource = self;
        _HOTCHATcollectionView.delegate = self;
        
        [_HOTCHATcollectionView registerClass:[PhotoVideoCell class] forCellWithReuseIdentifier:@"PhotoVideoCell"];
        
        _HOTCHATcollectionView.showsHorizontalScrollIndicator = NO;
        _HOTCHATcollectionView.backgroundColor = [UIColor clearColor];
        
    }
    return _HOTCHATcollectionView;
}
- (void)setModel:(UserInfoModel *)model{
    
    _model = model;
    
    [self.HOTCHATcollectionView reloadData];
}
#pragma mark UICollectionDatasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.model.privteArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PhotoVideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoVideoCell" forIndexPath:indexPath];
    
    cell.tag = indexPath.row;
    cell.model = [self.model.privteArr objectAtIndex:indexPath.row];
    
    return cell;
}
#pragma mark UICollectionview Delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
    
    PhotoVideoCell *cell = (PhotoVideoCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if (cell.model.isVideo) {
        void (^lookblock)(void) = ^{
            XMPlayerView *playerManager = [[XMPlayerView alloc] init];
            playerManager.sourceImagesContainerView = (UIView *)cell.HOTCHATbackImgView;
            // 当前的View
            playerManager.currentImage = cell.HOTCHATbackImgView.image;  // 当前的图片
            NSString *url = [NSString stringWithFormat:@"%@%@",APIHEADER,cell.model.url];
            playerManager.videoURL = [NSURL URLWithString:url];
            [playerManager show];
            

        };
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(HOTCHATlookVideo:lookBlock:)]) {
            [self.delegate HOTCHATlookVideo:cell.model.url.md5 lookBlock:lookblock];
        }
        return;
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(HOTCHATlookPhotoIndex:type:)]) {
        [self.delegate HOTCHATlookPhotoIndex:indexPath.row type:10001];
    }
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 8;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 8;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(75, 75);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 15, 0, 15);
}
@end
